package com.sfh.cp.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.home.HomeMenuData;
import com.sfh.cp.mass.adapter.MassPageAdapter;
import com.sfh.cp.mass.child.GroomFragment;
import com.sfh.cp.mass.child.NewestFragment;
import com.sfh.cp.mass.pst.IPstMassInfo;
import com.sfh.cp.user.login.UserLoginActivity;
import com.sfh.cp.widget.WidgetBaseToolbar;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crm.sfh.server.Const;
import crm.sfh.server.SUser;
import crm.sfh.server.entity.EMass;

/**
 * Created by Administrator on 2018/3/14.
 */

public class MassActivity extends AbstractBaseActivity implements IPstMassInfo.IVMassInfo {



    public static void startUI(Context context, EMass mass) {
        Intent intent = new Intent(context, MassActivity.class);
        intent.putExtra("DATA", mass.getMassId());
        context.startActivity(intent);
    }

    MassPageAdapter adapter;
    @BindView(R.id.ivHead)
    ImageView ivHead;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    WidgetBaseToolbar toolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.btAdd)
    ImageView btAdd;


    EMass mass;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_activity);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        toolbar.setBackFinish();
        String massId = getIntent().getStringExtra("DATA");
        adapter = new MassPageAdapter(getSupportFragmentManager());
        adapter.setFragmentList(new HomeMenuData(GroomFragment.newFragment(massId), "最热"));
        adapter.setFragmentList(new HomeMenuData(NewestFragment.newFragment(massId), "最新"));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(adapter);
        pstMassInfo.getInfo(massId);

    }

    @Override
    public void onSuccessInfo(EMass mass) {
        this.mass = mass;
        this.toolbar.setTitle(mass.getName());
        tvTitle.setText(mass.getContent());
        Picasso.with(this).load(mass.getHead()).into(ivHead);
    }

    IPstMassInfo pstMassInfo;

    @Override
    public IPresenter getPresenter() {
        if (pstMassInfo == null) {
            pstMassInfo = LoaderService.getService(IPstMassInfo.class);
        }
        return pstMassInfo;
    }

    @OnClick(R.id.btAdd)
    public void onViewClicked() {

        SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        if (TextUtils.isEmpty(user.getObjectId())) {
            UserLoginActivity.startUI(this);
            return;
        }
        MassAddContentActivity.startUI(this, mass);
    }

    float y1 = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                y1 = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float y2 = ev.getY();
                if (y1 - y2 > 50) {
                    //向上滑
                    if (btAdd.isShown()){
                        btAdd.setVisibility(View.GONE);
                    }

                } else if (y2 - y1 > 50) {
                    //向下滑
                    if (!btAdd.isShown()){
                        btAdd.setVisibility(View.VISIBLE);
                    }

                }
                break;
            case MotionEvent.ACTION_UP:
                y1 = 0;
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
